<?php

namespace app\branchoffice\model;

use think\Model;


class Goods extends Model
{

    

    

    // 表名
    protected $name = 'goods';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'departtime_text',
        'uptotime_text',
        'switch_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getSwitchList()
    {
        return ['1' => __('Switch 1'), '0' => __('Switch 0')];
    }


    public function getDeparttimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['departtime']) ? $data['departtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUptotimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['uptotime']) ? $data['uptotime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getSwitchTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['switch']) ? $data['switch'] : '');
        $list = $this->getSwitchList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setDeparttimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUptotimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function goodscategory()
    {
        return $this->belongsTo('GoodsCategory', 'category_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function branchofficeoffice()
    {
        return $this->belongsTo('BranchofficeOffice', 'office_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function manystoreshop()
    {
        return $this->belongsTo('ManystoreShop', 'shop_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function manystore()
    {
        return $this->belongsTo('Manystore', 'store_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
